package com.pipi.biyesheji.service.impl;

import com.pipi.biyesheji.dao.manager.CategoryManager;
import com.pipi.biyesheji.dao.model.CategoryDO;
import com.pipi.biyesheji.dao.query.CategoryQuery;
import com.pipi.biyesheji.dao.support.PageInfo;
import com.pipi.biyesheji.dao.support.PageResult;
import com.pipi.biyesheji.enums.BooleanEnum;
import com.pipi.biyesheji.pojo.Category;
import com.pipi.biyesheji.pojo.Product;
import com.pipi.biyesheji.service.CategoryService;
import com.pipi.biyesheji.service.ProductService;
import com.pipi.biyesheji.util.BeanUtils;
import com.pipi.biyesheji.util.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pipi
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryManager categoryManager;
    @Autowired
    private ProductService productService;

    @Override
    public PageResult<Category> list(Page page) {
        CategoryQuery query = new CategoryQuery();
        query.createCriteria().andIsDeletedEqualTo(BooleanEnum.NO.getCode());
        List<CategoryDO> categoryDOList = categoryManager.selectByQuery(query);
        if (CollectionUtils.isEmpty(categoryDOList)) {
            return PageResult.createPageResult();
        }
        List<Category> categories = buildCategorys(categoryDOList);
        return PageResult.createPageResult(categories);
    }

    @Override
    public void save(Category category) {
        CategoryDO categoryDO = BeanUtils.createCloneBean(category, CategoryDO.class);
        categoryDO.setIsDeleted(BooleanEnum.NO.getCode());
        categoryManager.insertSelective(categoryDO);
    }

    @Override
    public void del(Long id) {
        List<Product> products = productService.findByCid(id);
        if(CollectionUtils.isEmpty(products)){
            categoryManager.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Category get(Long id) {
        CategoryDO categoryDO = categoryManager.selectByPrimaryKey(id);
        return buildCategory(categoryDO);
    }

    @Override
    public void update(Category category) {
        CategoryDO categoryDO = BeanUtils.createCloneBean(category, CategoryDO.class);
        categoryManager.updateByPrimaryKeySelective(categoryDO);
    }

    @Override
    public List<Category> getCategoryByUserId(Long userId) {
        CategoryQuery categoryQuery = new CategoryQuery();
        categoryQuery.createCriteria().andUserIdEqualTo(userId);
        List<CategoryDO> categoryDOList = categoryManager.selectByQuery(categoryQuery);
        return buildCategorys(categoryDOList);
    }

    private List<Category> buildCategorys(List<CategoryDO> categoryDOList) {
        return categoryDOList.stream().map(this::buildCategory).collect(Collectors.toList());
    }

    private Category buildCategory(CategoryDO categoryDO) {
        return BeanUtils.createCloneBean(categoryDO, Category.class);
    }
}
